package com.guigu.springcloud.controller;

import cn.hutool.core.util.IdUtil;
import com.guigu.springcloud.common.CommonResult;
import com.guigu.springcloud.entity.Payment;
import com.guigu.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/payment")
@Slf4j
// 设置默认降级处理方法
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping
    public CommonResult create(@RequestBody Payment payment) {
        log.info(payment.toString());
        int result = paymentService.create(payment);
        return CommonResult.voData(200, "创建成功" + serverPort, result);
    }

    @GetMapping(value = "/{id}")
    public CommonResult findById(@PathVariable("id") Long id) {
        log.info(id.toString());
        Payment payment = paymentService.findById(id);
        log.info(payment.toString());
        return CommonResult.voData(200, "查询成功" + serverPort, payment);
    }

    @GetMapping("/discovery")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("****服务名***" + service);
            List<ServiceInstance> instances = discoveryClient.getInstances(service);
            for (ServiceInstance instance : instances) {
                log.info("--该服务信息---" + instance.getInstanceId() + "\t" + instance.getHost() + "\t" +
                        instance.getPort() + "\t" + instance.getUri());

            }
        }
        return discoveryClient;
    }

    @GetMapping("/myloadBalance")
    public String getServicePort() {
        return serverPort;
    }


    @GetMapping(value = "/hystrix/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
            })
    public String testHystrix_Ok(@Param("id") Long id) {
        System.out.println(9 / 0);
        return paymentService.testHystrix_Ok(id);
    }



    public String paymentTimeOutFallbackMethod() {
        return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
    }


    private String payment_Global_FallbackMethod() {
        return "请求失败，请重新发送请求";
    }


    @GetMapping(value = "/hystrix")
    public String testHystrix_Timeout() {
        return paymentService.testHystrix_Timeout();
    }

    // 服务熔断
    @GetMapping("/circuitBeaker/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        return paymentService.paymentCircuitBreaker(id);
    }
}
